import IORedis, {RedisKey} from "ioredis";
import Fn from "../../common/Fn";

export class LRedis extends IORedis {
    async setJson(key: RedisKey, value: object, second: number = -1): Promise<"OK"> {
        let str = JSON.stringify(value);
        if (second > 0) {
            return super.set(key, str, "EX", second);
        }
        return super.set(key, str);
    }

    async getJson<T>(key: RedisKey): Promise<T | null> {
        let str = await super.get(key);
        if (str == null) {
            return null;
        }
        return Fn.safeJson(str);
    }
}